package com.yxplearn.mybatisplus.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yxplearn.mybatisplus.mapper.UserMapper;
import com.yxplearn.mybatisplus.mapper.UserRecordMapper;
import com.yxplearn.mybatisplus.pojo.User;
import com.yxplearn.mybatisplus.pojo.UserRecord;
import com.yxplearn.mybatisplus.util.QPSCalculator;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.Executors;

@Log4j2
@Controller
public class RecordController {

    @Autowired
    UserRecordMapper userRecordMapper;
    @Autowired
    UserMapper userMapper;


    @RequestMapping("/insertRecord")
    @ResponseBody
    public String insertRecord() {
//        List<Integer> integers = Arrays.asList(1, 1, 1);
//        Map<Integer, Integer> map = new HashMap<>();
//        map.put(1, 2);
//        UserRecord userRecord = new UserRecord();
//        userRecord.setId(IdWorker.getId());
//        userRecord.setPlayerName("yxp1");
//        userRecord.setMapValue(map);
//        userRecord.setListValue(integers);
//        userRecordMapper.insertOrUpdate(userRecord);
        try {
            Thread.sleep(10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        QPSCalculator.getInstance().pass();
        return "ok";
    }

    @RequestMapping("/insertAllRecord")
    @ResponseBody
    public String replaceIntoRecord() {

        List<UserRecord> recordList = new ArrayList<>();
        List<Integer> integers = Arrays.asList(1, 1, 1);
        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 2);
        for (int i = 0; i < 2; i++) {
            UserRecord userRecord = new UserRecord();
            userRecord.setId(IdWorker.getId());
            userRecord.setPlayerName("yxp1");
            userRecord.setMapValue(map);
            userRecord.setListValue(integers);
            recordList.add(userRecord);
        }
        userRecordMapper.replaceIntoList(recordList);
        return "ok";
    }

    @RequestMapping("/updateRecord0")
    @ResponseBody
    public String updateRecord0() {
        UserRecord record = userRecordMapper.selectById(1442756268592218114l);
        userRecordMapper.updateById(record);
        QPSCalculator.getInstance().pass();
        return "ok";
    }

    @RequestMapping("/updateRecord")
    @ResponseBody
    public String updateRecord(@RequestParam("id") long id) {
        UserRecord record = userRecordMapper.selectById(id);
        userRecordMapper.updateById(record);
        return "ok";
    }

    @RequestMapping("/updateRecord2")
    @ResponseBody
    public String updateRecord2(@RequestParam("id") long id) {
        UserRecord record = new UserRecord();
        record.setId(id);
        record.setPlayerName("ffs");
        userRecordMapper.updateById(record);
        return "ok";
    }

    @RequestMapping("/updateRecord3")
    @ResponseBody
    public String updateRecord3() {
        UserRecord record = new UserRecord();
        long id = IdWorker.getId();
        record.setId(IdWorker.getId());
        record.setPlayerName("ffs");
        userRecordMapper.insert(record);
        return "ok";
    }

    @RequestMapping("/updateRecord4")
    @ResponseBody
    public String updateRecord4() {
        Executor executor = Executors.newFixedThreadPool(10);
        List<UserRecord> userRecords = userRecordMapper.selectList(null);

        ExecutorCompletionService completionService = new ExecutorCompletionService<>(executor);
        final String name = "83e2";
        for (UserRecord userRecord : userRecords) {
            completionService.submit(() -> {

                userRecord.setPlayerName(name);
                userRecordMapper.updateById(userRecord);
                log.info(Runtime.getRuntime().toString());
            }, null);
        }
        return "ok";
    }

    @RequestMapping("/deleteRecord")
    @ResponseBody
    public String deleteRecord(@RequestParam("id") long id) {
        int i = userRecordMapper.deleteById(id);
        return "ok";
    }

    @RequestMapping("/deleteRecordIds")
    @ResponseBody
    public String deleteRecordIds() {
        List<UserRecord> yxp1 = userRecordMapper.selectList(Wrappers.<UserRecord>lambdaQuery().eq(UserRecord::getPlayerName, "yxp1"));
        List<Long> ids = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            ids.add(yxp1.get(i).getId());
        }
        int i = userRecordMapper.deleteBatchIds(ids);
        return "ok";
    }

    @RequestMapping("/replaceRecord")
    @ResponseBody
    public String replaceRecord(@RequestParam("id") long id) {
        List<UserRecord> recordList = new ArrayList<>();
        List<Integer> integers = Arrays.asList(1, 1, 1);
        Map<Integer, Integer> map = new HashMap<>();

        map.put(1, 2);
        UserRecord userRecord = new UserRecord();
        if (id == 0) {
            userRecord.setId(IdWorker.getId());
            userRecord.setPlayerName("yxp1");
            userRecord.setMapValue(map);
            userRecord.setListValue(integers);
            recordList.add(userRecord);
        } else {
            userRecord.setId(id);
            userRecord.setPlayerName("yxp2");
        }
        userRecordMapper.replaceInto(userRecord);

        return "ok";
    }

    @RequestMapping("/insertOrUpdate")
    @ResponseBody
    public String insertOrUpdate(@RequestParam("id") long id) {
        List<UserRecord> recordList = new ArrayList<>();
        List<Integer> integers = Arrays.asList(1, 1, 1);
        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 2);
        UserRecord userRecord = new UserRecord();
        if (id == 0) {
            userRecord.setId(IdWorker.getId());
            userRecord.setPlayerName("yxp1");
            userRecord.setMapValue(map);
            userRecord.setListValue(integers);
            recordList.add(userRecord);
        } else {
            userRecord.setId(id);
            userRecord.setPlayerName("yxp2");
        }
        userRecordMapper.insertOrUpdate(userRecord);
        return "ok";
    }

    @RequestMapping("/selectRecord")
    @ResponseBody
    public String selectRecord() {
        UserRecord userRecord = userRecordMapper.selectById(1442756267942100993l);
        QPSCalculator.getInstance().pass();
        return userRecord.toString();
    }

    @RequestMapping("/replaceRecordOrError")
    @ResponseBody
    public String replaceRecord() {
        List<UserRecord> recordList = new ArrayList<>();
        List<Integer> integers = Arrays.asList(1, 1, 1);
        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 2);

        UserRecord userRecord1 = new UserRecord();
        userRecord1.setId(1l);
        userRecord1.setPlayerName("yxp1");
        userRecord1.setMapValue(map);
        userRecord1.setListValue(integers);
        userRecord1.setIsExist(0);
        userRecord1.setCreateTime(new Date());
        userRecord1.setModifyTime(new Date());
        recordList.add(userRecord1);

        UserRecord userRecord2 = new UserRecord();
        userRecord2.setId(2l);
        userRecord2.setPlayerName("yxp2");
        userRecord2.setMapValue(map);
        userRecord2.setListValue(integers);
        userRecord2.setIsExist(0);
        userRecord2.setCreateTime(new Date());
        userRecord2.setModifyTime(new Date());
        recordList.add(userRecord2);

        userRecordMapper.replaceIntoList(recordList);
        return "ok";
    }

    @RequestMapping("/selectCostTest")
    @ResponseBody
    public String selectCostTest(@RequestParam("id") long id) {
        long beginTime = System.currentTimeMillis();
        UserRecord userRecord = userRecordMapper.selectById(id);
        long endTime = System.currentTimeMillis();
        System.out.println(endTime - beginTime);
        return "ok";
    }
}
